package algo;

import java.util.Scanner;

public class Tieta {

    public static void main(String args[]) {

//        Scanner input = new Scanner(System.in);
//        int len = input.nextInt();
//        int[] nums = new int[len];
//        int i = 0;
//        while (input.hasNext()) {
//            int value = input.nextInt();
//            nums[i++] = value;
//        }
        int test1 = new Tieta().calcMoney(8, new long[]{1, 9, 9, 4, 1, 2, 2, 9});
        System.out.println(test1);
//
//        int test2 = new Tieta().calcMoney(5, new long[]{1,1,1,1,1});
//        System.out.println(test2);
//
//        int test3 = new Tieta().calcMoney(0, new long[]{});
//        System.out.println(test3);
//
//        int test4 = new Tieta().calcMoney(8, new long[]{9, 1, 1, 1, 1, 1, 1, 1});
//        System.out.println(test4);
//
//        int test5 = new Tieta().calcMoney(9, new long[]{2147483647, 2147483647, 1, 2147483647, 1, 1, 1, 4,9});
//        System.out.println(test5);

//        int test6 = new Tieta().calcMoney(8, new long[]{1, 3, 1, 1, 9, 9, 9, 9});
//        System.out.println(test6);

        int test7 = new Tieta().calcMoney(8, new long[]{1, 3, 1, 1, 1, 1, 5, 5});
        System.out.println(test7);

    }

    public int calcMoney(int len, long[] nums) {
        if (len == 0) return 0;
        int l = 0, r = 1;
        long sum = 0;
        while (l < len && r < len && l < r) {
            sum += nums[r];
            if (sum < nums[l]) {
                r++;
                if(r<len && (sum+nums[l])<=nums[r]){
                    nums[l]+=sum;
                    sum=0;
                    System.out.println(l+ " , add to left value: "+nums[l]);
                };
            } else {
                l++;
                r++;
                nums[l] = sum;
                sum = 0;
            }

        }

        for (int i = 0; i <= l; i++) {
            System.out.print(" "+nums[i]);
        }

        System.out.println("");

        int ans = len - l - 1;
//        System.out.println(ans);
        return ans;
    }

}
